/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.application.das;

import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.RoleBO;
import cn.hallele.ms.portalcenter.domain.action.role.RoleDomainService;
import cn.hallele.ms.portalcenter.domain.domain.RoleDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/6 13:51
 */
@Slf4j
@Service
public class RoleBDDas extends ModelDas<RoleDomainService, RoleBO, RoleDomain> {
    @Autowired
    private MenuBDDas menuBDDas;

    protected RoleBDDas(RoleDomainService option) {
        super(option);
    }

    /**
     * 通过用户id查询角色信息
     *
     * @param userId 用户id
     * @return 角色信息
     */
    public List<RoleBO> findByUserId(Long userId) {
        return revertList(this.option.findByUserId(userId));
    }

    /**
     * 暴露出去用的convert
     *
     * @param roleBOS roleBOS
     * @return MenuEntity
     */
    public Set<RoleDomain> convert(Set<RoleBO> roleBOS) {
        return ObjectUtil.toSet(convertList(ObjectUtil.toList(roleBOS)));
    }

    /**
     * 暴露出去用的revert
     *
     * @param roleDomains roleDomains
     * @return MenuDomain
     */
    public Set<RoleBO> revert(Set<RoleDomain> roleDomains) {
        return ObjectUtil.toSet(revertList(ObjectUtil.toList(roleDomains)));
    }

    /**
     * 将T转成E
     *
     * @param roleBO 被转换者
     * @return S 转换后者
     */
    @Override
    protected RoleDomain convert(RoleBO roleBO) {
        return ObjectUtil.map(roleBO, u -> RoleDomain.builder()
                .id(u.getId())
                .name(u.getName())
                .code(u.getCode())
                .sort(u.getSort())
                .status(u.getStatus())
                .dataScope(u.getDataScope())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .menus(menuBDDas.convert(u.getMenus()))
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param roleDomain 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected RoleBO revert(RoleDomain roleDomain) {
        return ObjectUtil.map(roleDomain, u -> RoleBO.builder()
                .id(u.getId())
                .name(u.getName())
                .code(u.getCode())
                .sort(u.getSort())
                .status(u.getStatus())
                .dataScope(u.getDataScope())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .menus(menuBDDas.revert(u.getMenus()))
                .build());
    }
}
